package com.pricetag.components;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;

import com.pricetag.exceptions.SearchRecordNotExistsException;
import com.pricetag.helpers.Constants;
import com.pricetag.pricetagandroid.R;
import com.pricetag.pricetagandroid.SearchDetailActivity;
import com.pricetag.pricetagandroid.SearchDetailTabActivity;
import com.pricetag.pricetagandroid.SearchHistoryListActivity;
import com.pricetag.search.SearchHandler;
import com.pricetag.search.SearchRecord;

public class SearchHistoryListAdapter extends ArrayAdapter<String> {

	private Context context;
	private Activity activity;
	private SearchRecord[] searchRecords;
	private SearchHistoryListAdapter me;

	public SearchHistoryListAdapter(Context context, String[] dummyRecords, SearchRecord[] searchRecords) {
		super(context, R.layout.view_search_history_row, dummyRecords);
		me = this;
		if (dummyRecords.length != searchRecords.length) {
			Log.w("SearchHistoryListAdapter", "Records sizes don't match");
		}
		this.context = context;
		if (context instanceof Activity) {
			this.activity = (Activity) context;
		}

		this.searchRecords = searchRecords;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View rowView = inflater.inflate(R.layout.view_search_history_row, parent, false);

		TextView dateView = (TextView) rowView.findViewById(R.id.search_history_row_date_textview);
		final TextView contentView = (TextView) rowView.findViewById(R.id.search_history_row_content_textview);
		ImageView imageView = (ImageView) rowView.findViewById(R.id.search_history_row_thumbnail_imageview);

		final SearchRecord searchRecord = searchRecords[position];
		final String searchRecordID = searchRecord.getSearchRecordID();

		// Image view
		Bitmap thumbnail = BitmapFactory.decodeByteArray(searchRecord.getThumbnail(), 0,
				searchRecord.getThumbnail().length);
		imageView.setImageBitmap(thumbnail);
		imageView.getLayoutParams().height = 150;
		imageView.getLayoutParams().width = 100;

		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ShowImageDialogFragment dialog = new ShowImageDialogFragment(searchRecordID);
				if (activity instanceof FragmentActivity) {
					FragmentActivity fragmentActivity = (FragmentActivity) activity;
					dialog.show(fragmentActivity.getSupportFragmentManager(), "dialog");
				}
			}
		});

		// Date view
		dateView.setText(searchRecord.getDateCreated().toLocaleString());

		// Content view
		String productNameMessage = "";
		String localName = searchRecord.getLocalImageDBProductName();
		String googleName = searchRecord.getGoogleGoggleProductName();
		String iqeName = searchRecord.getIqEngineProductName();
		if (localName != null) {
			productNameMessage = localName;
		} else if (iqeName != null) {
			productNameMessage = iqeName;
		} else if (googleName != null) {
			productNameMessage = googleName;
		} else {
			productNameMessage = "We are working on it ...";
		}
		contentView.setText(productNameMessage);

		contentView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				contentView.setBackgroundColor(Color.LTGRAY);				
				Intent i = new Intent(context, SearchDetailTabActivity.class);
				i.putExtra(Constants.SEARCH_RECORD_ID, searchRecordID);
				context.startActivity(i);
			}

		});

		contentView.setLongClickable(true);
		contentView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
				menu.add("Delete");
				menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener() {
					@Override
					public boolean onMenuItemClick(MenuItem item) {
						searchRecord.setDeleted(true);
						if (activity instanceof SearchHistoryListActivity) {
							((SearchHistoryListActivity) activity).onDelete();
						}
						return true;
					}
				});

				menu.add("Search again");
				menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener() {
					@Override
					public boolean onMenuItemClick(MenuItem item) {
						try {
							new SearchHandler(searchRecordID, context).startImageSearchAgain();
							Toast.makeText(context, "Start to search again...", Toast.LENGTH_SHORT).show();
						} catch (SearchRecordNotExistsException e) {
						}
						return true;
					}
				});

			}
		});
		contentView.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				contentView.showContextMenu();
				return true;
			}
		});
		
		return rowView;
	}
}
